package com.netease.nim.uikit.xpopup.popuview

import android.content.Context
import android.view.View
import android.widget.ImageView
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import com.king.zxing.util.CodeUtils
import com.lxj.xpopup.core.BottomPopupView
import www.wen.com.common.R
import www.wen.com.common.lib.data.entities.ProtocolEntity
import www.wen.com.common.lib.extension.glide.setImageUrl
import www.wen.com.common.lib.extension.imgUrl
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.helper.ResultUiCallBack
import www.wen.com.common.lib.util.DisplayUtil
import java.util.*


/**
 * @author WEN
 * @Description: 底部分享的弹窗
 * @date 2021/8/19 14:41
 */
class BottomSharePosterPopupView(
    context: Context,
    private val content: ProtocolEntity.Content,
    private val resultCallBack: ResultUiCallBack<ProtocolEntity.Content>,
    ) : BottomPopupView(context), View.OnClickListener {

    private val hints: MutableMap<EncodeHintType, Any> = HashMap<EncodeHintType, Any>().apply {
        put(EncodeHintType.CHARACTER_SET, "utf-8")
        put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H)
        put(EncodeHintType.MARGIN, 0)
    }

    private lateinit var save: View

    private lateinit var ivPoster: ImageView

    lateinit var clPoster: View

    private lateinit var ivPosterQrCode: ImageView

    override fun getImplLayoutId(): Int = R.layout.popupview_poster

    override fun onCreate() {
        super.onCreate()
        save = findViewById(R.id.save)
        ivPosterQrCode = findViewById(R.id.ivPosterQrCode)
        ivPoster = findViewById(R.id.ivPoster)
        clPoster = findViewById(R.id.clPoster)
        setImageUrl(ivPoster,content.content.imgUrl())

        val bitmap = CodeUtils.createBarCode(content.shareUrl, BarcodeFormat.QR_CODE, DisplayUtil.dip2px(100f).toInt(),DisplayUtil.dip2px(100f).toInt(), hints)
        ivPosterQrCode.setImageBitmap(bitmap)

        setOnClickListenerAgency(this, save)
    }

    override fun onClick(v: View) {
        resultCallBack.invoke(v,content,this)
    }

}